/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.model;

import javax.swing.text.Element;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTML.Attribute;
import offset.nodes.Constants;
import offset.nodes.client.editor.Editor;

/**
 *
 * @author Walter Lütgenau
 */
public class ImageModel extends EditorModel {

    Editor editor;

    public ImageModel(DocumentContext context, Editor editor) {
        super(context);
        this.editor = editor;
    }

    public Editor getEditor() {
        return editor;
    }

    /**
     * Insert an image element  referring a node representing an image.
     *
     * @param range The range within the document, that shall be substituted by the image
     * @param path the path to the image node
     * @throws java.lang.Exception
     */
    public void insertRepositoryImage(Range range, String uuid) throws Exception {
        if (uuid == null || uuid.length() == 0)
            return;

        Element e = getRealParagraph(range.getStartOffset());
        if (getRealParagraph(range.getEndOffset()) != e)
            return; // can only insert as a span into a paragraph element

        // workaround for swing bug with p-implied paragraphs
        e = getParagraphWithNoImpliedSiblings(e);

        String image = null;

        image = "<" + Tag.IMG.toString()
                + " "
                + Attribute.SRC.toString()
                + "=\"" + HyperlinkModel.REPOSITORY_URL + Constants.QUERY_SEPARATOR + Constants.PAR_DATA_REFERENCE + "=" + uuid
                + "\"/>";

        String html = substituteRange(e, image, range.getStartOffset(), range.getEndOffset());

        setOuterHTML(e, html);
    }

    /**
     * Insert an image element  referring to a local file, that has been uploaded in the same location as the HTML page.
     *
     * @param range The range within the document, that shall be substituted by the image
     * @param path the path to the image node
     * @param context the URL context
     * @throws java.lang.Exception
     */
    public void insertLocalImage(Range range, String uuid) throws Exception {
        if (uuid == null || uuid.length() == 0)
            return;

        Element e = getRealParagraph(range.getStartOffset());
        if (getRealParagraph(range.getEndOffset()) != e)
            return; // can only insert as a span into a paragraph element

        // workaround for swing bug with p-implied paragraphs
        e = getParagraphWithNoImpliedSiblings(e);

        String path = (String) editor.getProperties().get(Editor.PROP_DOCUMENT_PATH);
        path = path.replaceAll(" ", "+");

        // TODO: Find some src reference, that does not include the path of the HTML page. If a component of the path changes, the image will become invalid.
        String image = "<" + Tag.IMG.toString()
                + " "
                + Attribute.SRC.toString()
                + "=\"" + HyperlinkModel.REPOSITORY_URL + Constants.QUERY_SEPARATOR + Constants.PAR_DATA_REFERENCE + "=" + uuid
                + "\"/>";

        String html = substituteRange(e, image, range.getStartOffset(), range.getEndOffset());

        setOuterHTML(e, html);
    }

}
